package itcast.bos.base.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.crm.domain.Customer;
import itcast.bos.base.action.base.BaseAction;
import itcast.bos.base.service.ICourierService;
import itcast.bos.base.service.IFixedAreaService;
import itcast.bos.base.service.ITakeTimeService;
import itcast.bos.domain.base.Courier;
import itcast.bos.domain.base.FixedArea;
import itcast.bos.domain.base.TakeTime;

@ParentPackage("json-default")
@Controller
@Scope("prototype")
@Namespace("/")
public class FixedAreaAction extends BaseAction<FixedArea> {

	@Autowired
	private IFixedAreaService ias;
	@Autowired
	private ICourierService is;
	@Autowired
	private ITakeTimeService itt;
	//从form表单中拿到关联快递员的参数
	//private String id;
	private String courierId;
	private String takeTimeId;
	
	
	
	private String[] customerIds;

	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}
	//关联快递员
	@Action(value = "doAccFixedAreaAndCourier", results = { @Result(name = "success", type = "redirect",location="./pages/base/fixed_area.html") })
	public String doAccFixedAreaAndCourier() {
		//System.out.println("id---"+model.getId()+"---courierId---"+courierId+"--takeTimeId---"+takeTimeId);
		//查找到这个快递员和这个取派时间
		Courier c=is.findOne(courierId);
		TakeTime tt=itt.findOne(takeTimeId);
		//让快递员设置一个取派时间
		//is.updateCourierByTime(courierId,tt);
		c.setTakeTime(tt);
		//关联快递员和定区
		ias.doAcc(model,c);
		
		return SUCCESS;
	}
	//查找取件标准
	@Action(value = "findtakeTimeids", results = { @Result(name = "success", type = "json") })
	public String findTakeTimes() {
		List<TakeTime> list1=itt.findAll();
		ActionContext.getContext().getValueStack().push(list1);
		return SUCCESS;
	}
	//查找没有关联定去的快递员
		@Action(value = "findcouriers", results = { @Result(name = "success", type = "json") })
		public String findNoFixedAreaCustomers() {
			List<Courier> list=is.findNoFixedAreaCouriers();
			ActionContext.getContext().getValueStack().push(list);
			System.out.println(list);
			return SUCCESS;
		}
	
	
	//关联定去
	@Action(value = "doAccCustomer", results = { @Result(name = "success", type = "redirect",location="./pages/base/fixed_area.html") })
	public String doAccCustomers() {
		String fixedAreaId = model.getId();
		String sts = StringUtils.join(customerIds,",");
		System.out.println("客户的id的组合----"+sts+"定去的fixedAreaId---->"+fixedAreaId);
		
		 WebClient.create("http://localhost:9995/crm_management/services/customerService/doAcc?str="+sts+"&FixedAreaId="+fixedAreaId)
				.accept(MediaType.APPLICATION_JSON).put(null);
		
		return SUCCESS;
	}
	
	//查找未关联的用户
	@Action(value = "findNoAccCustomer", results = { @Result(name = "success", type = "json") })
	public String findNoAccCustomers() {
		Collection<? extends Customer> nolist = WebClient
				.create("http://localhost:9995/crm_management/services/customerService/noAcc")
				.accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(nolist);
		//System.out.println("未关联----------------------" + nolist);
		return SUCCESS;
	}
	//查找已关联的用户
	@Action(value = "findAccCustomer", results = { @Result(name = "success", type = "json") })
	public String findAccCustomers() {
		//System.out.println("ID--------------" + model.getId());
		Collection<? extends Customer> list = WebClient
				.create("http://localhost:9995/crm_management/services/customerService/Acc/" + model.getId())
				.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(list);
		//System.out.println("已关联" + list);
		return SUCCESS;
	}
	//保存定去
	@Action(value = "saveFixed_area", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/fixed_area.html") })
	public String add() {
		/*System.out.println(model);
		String s = UUID.randomUUID().toString().replace("-", "");*/
		// model.setId(s);
		//System.out.println(model);
		ias.save(model);
		return SUCCESS;
	}
	//分页查询
	@Action(value = "findFixed_area", results = { @Result(name = "success", type = "json") })
	public String findFixed() {
		//System.out.println("进来了。。。。。。。。。。。");
		Pageable pageable = new PageRequest(page - 1, rows);
		// System.out.println("pageable "+pageable);
		Specification<FixedArea> sf = new Specification<FixedArea>() {

			@Override
			public Predicate toPredicate(Root<FixedArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();
				if (StringUtils.isNotBlank(model.getId())) {
					Predicate q1 = cb.equal(root.get("id").as(String.class), model.getId());
					list.add(q1);
				}
				if (StringUtils.isNotBlank(model.getCompany())) {
					Predicate p2 = cb.like(root.get("company").as(String.class), "%" + model.getCompany() + "%");
					list.add(p2);
				}
				// return null;
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
	//	System.out.println("sf             " + sf);
		//System.out.println("pageable          " + pageable);
		Page<FixedArea> result = ias.findAll(sf, pageable);
		//System.out.println(result);
		pushvaluestack(result);
		return SUCCESS;
	}

	public String getCourierId() {
		return courierId;
	}
	public void setCourierId(String courierId) {
		this.courierId = courierId;
	}
	public String getTakeTimeId() {
		return takeTimeId;
	}
	public void setTakeTimeId(String takeTimeId) {
		this.takeTimeId = takeTimeId;
	}

}
